********************************************************************************
*
* Figure 1: Visual Evidence of Merger Effects on Hospital Inputs and Outcomes
*
* Outcomes: profit margin, vendor distance, and physician exits 
*
********************************************************************************



capture log close 
clear all 
log using log_event_study20230606.log, replace 



*******
******* Settings and directories 
*******

** specify an output directory 
local fpath_output "/homes/nber/shruthi-dua51934/sacarny-DUA51934/shruthi-dua51934/replication_files/output_20230606"

** directory containing the main analytic file
local fpath_build_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output"

** directory ccontaining himss supplemental variables 
local fpath_himss = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/himss/input/"

** directory containing physician flows supplemental data 
local fpath_physician_flows = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/physician_flows/output/"

** replication analysis directory 
local fpath_analysis = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/analysis_20230606/"



********
******** which event studies to run? 
********
* profit margin 
local run_profitmargin = 1

* vendor distance
local run_vendordist = 1

* physician exits
local run_physicianexit = 1

* physician entrants 
local run_physicianentrant = 1

* capital investment
local run_logcapinv = 1 

* log(FTE)
local run_fte= 1


*******
******* read and assemble the data 
*******
use "`fpath_build_output'/acq_cleaned_complete_20230606.dta", clear 

** clean up
label var hospbd "Total beds"

* reformat urbancbsa variable: 1 if rural 
bys id: egen urban2 = max(urbancbsa)
replace urbancbsa = urban2 
replace urbancbsa = 1 - urbancbsa 
label var urbancbsa "Share rural hospitals"

* drop if missing hrrcode
drop if missing(hrrcode)

* numeric variable for the aha id 
egen id2 = group(id)

* total cost per adjusted discharge 
gen adj_costs = totcost / ((1000000)*hospbd*(1 + (opcharge/ipcharge)))
label var adj_costs "Adjusted costs per bed (\$1 millions)" 
winsor2 adj_costs, cuts(05 95) by(year)

* total revenue per adjusted discharge 
gen adj_revenue = income / ((1000000)*hospbd*(1 + (opcharge/ipcharge)))
label var adj_revenue "Adjusted revenue per bed (\$1 millions)"
winsor2 adj_revenue, cuts(05 95) by(year)

* log costs and revenue 
gen ladjcosts_w = log((adj_costs_w))
label var ladjcosts_w "Log(costs/bed)"

gen ladjrev_w = log((adj_revenue_w))
label var ladjcosts_w "Log(revenue/bed)"

* price
winsor2 dafny_price, cuts(05 95) by(year)
gen ldafny_price05 = log(dafny_price_w)
label var ldafny_price05 "log(Dafny price index)"

* winsorize profit margin in each year
* this creates profit_margin_w //
winsor2 profit_margin, cuts(05 95) by(year) label

* capital investment 
replace capinv_tot = capinv_tot/1000000
label var capinv_tot "Capital investment"
gen capinv_tot_per_bed = capinv_tot / hospbd
label var capinv_tot_per_bed "Capital investment per bed"
* winsorize and log  capital investment
winsor2 capinv_tot, cuts(05 95) by(year) label
gen lw_capinv_tot = log(1+ capinv_tot_w)
label var lw_capinv_tot "log(winsorized capital investment)"


* scale FTE counts by beds 
gen fte_per_bed = fte/hospbd
label var fte_per_bed  "Full time employees per bed"

* create log(fte)
gen logfte = log(fte)
label var logfte "log(FTE)"
label var fte "Full Time Employees"


********
******** For the event study spec
******** 
**  restrict the sample: Acq Legacy, Target, Other for-profits 
keep if forprofit == 1 
drop if target2 == 1 | acq_other == 1 

label var ind08_11acq_legacy "Acquirer * 08-11"
label var ind12_14target "Target * 12-14"

* define post-treatment 
gen postm = ( year >= 2008)
gen postm_target = postm * target
gen postm_legacy = postm * acq_legacy 
label var postm_target "Post 2008 * Target"
label var postm_legacy "Post 2008 * Acquirer"

* define interim
gen interim = (year == 2007)
gen interim_target = interim * target 
gen interim_legacy = interim * acq_legacy

label var interim_target "Target 2007"
label var interim_legacy "Acquirer 2007"

* sanity check... 
assert target2 + acq_other == 0 

* event study
* generate year dummies 
tab year, gen(yr)

*interact year dummies with legacy/ target 
foreach v of varlist yr* {
	gen target_`v' = target * `v'	
	gen legacy_`v' = acq_legacy * `v'

}

* clean up the labels on group * year interactions 
foreach v of varlist target_yr* {
	local num = subinstr("`v'", "target_yr", "", 1) 
	local `num' %4.0f `num'
	local num = `num' + 2002
	label var `v' "Target * `num'"
}


foreach v of varlist legacy_yr* {
	local num = subinstr("`v'", "legacy_yr", "", 1) 
	local `num' %4.0f `num'
	local num = `num' + 2002
	label var `v' "Acquirer * `num'"
}

* drop dummy for 2006 (t-1)
drop *yr4

* gen year * hrr FEs
egen yrhrr = group(year hrrcode)



******** 
******** profit margin 
********
if `run_profitmargin' == 1 {
	eststo: reghdfe profit_margin_w target_yr* legacy_yr* , absorb(year id2) vce(cluster id2)
	qui regsave using "`fpath_output'/profit_margin_es", tstat pval ci level(95) replace
	}

	
	
	



******** 
******** vendor distance 
********
if `run_vendordist'==1 {
	
	* merge the vendor distance file 
	preserve
	merge 1:1 id year using "`fpath_himss'/vendor_distance_indiv2003_2014_v2.dta", keep(match)
	
	* run the regression and save 
	gen discvendcount = distlegacy^2	
	
	eststo: reghdfe discvendcount target_yr* legacy_yr* , absorb(year id2) vce(cluster id2)
	qui regsave  using "`fpath_output'/discvend_es", tstat pval ci level(95) replace	
		
	restore 
	
}



******** 
******** physician exits 
********
if `run_physicianexit' == 1 { 
	
	preserve 
	
	* bring in the physician flows info
	merge 1:1 id year using "`fpath_physician_flows'/physician_flows_data2003_2014", keepusing(wpat* wcost* *_sh nphyshosp) keep(match) nogen 
	
	drop if year == 2008
	
	* physician exits 
	eststo: reghdfe wcost_exits target_yr* legacy_yr* , absorb(year id2) vce(cluster id2)
	qui regsave using "`fpath_output'/wcostexit_es", tstat pval ci level(95) replace
	
	
	restore 
	
}



******** 
******** physician entrants
********
if `run_physicianentrant' == 1 { 
	
	* bring in the physician flows info
	merge 1:1 id year using "`fpath_physician_flows'/physician_flows_data2003_2014", keepusing(wpat* wcost* *_sh nphyshosp) keep(match) nogen 
	
	drop if year == 2008
	
	* physician exits 
	eststo: reghdfe wcost_entrants target_yr* legacy_yr* , absorb(year id2) vce(cluster id2)
	qui regsave using "`fpath_output'/wcostenrant_es", tstat pval ci level(95) replace	
	
	}


	
******** 
******** capital investment 
********	
if `run_logcapinv' == 1 {
	eststo: reghdfe lw_capinv_tot target_yr* legacy_yr* if year > 2003, absorb(year id2) vce(cluster id2)
	qui regsave using "`fpath_output'/lcapinvw_es", tstat pval ci level(95) replace

	}
	

	
******** 
******** full time employees (FTE) per bed
********
if `run_fte' == 1 {	
	* run the regression
	eststo: reghdfe logfte  target_yr* legacy_yr* , absorb(year id2) vce(cluster id2)	
	qui regsave using "`fpath_output'/logfte_es", tstat pval ci level(95) replace
	}	
	
	

log close 
